package com.uxhao.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uxhao.commonutils.R;
import com.uxhao.eduservice.entity.EduTeacher;
import com.uxhao.eduservice.entity.query.TeacherQuery;
import com.uxhao.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教师管理控制器
 * @author uxhao
 * @since 2022/01/07
 */
@Api(tags={"教师管理"})
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;


    @ApiOperation("所有教师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<EduTeacher> list =teacherService.list(null);

        //测试自定义异常
        /*try {
            int i=10/0;
        } catch (Exception e) {
            throw  new QLLException(20002,"出异常了");
        }*/
        return R.ok().data(list);
    }

    @ApiOperation("根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id",value = "教师ID",required = true)
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        return flag ? R.ok() : R.error().message("删除失败");
    }

    //current 当前页
    //limit 每页记录数
    @ApiOperation("分页查询教师的方法")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");

        //int i = 10/0;

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);
    }



    @ApiOperation("条件查询带分页方法")
    @PostMapping("pageListTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) TeacherQuery teacherQuery) {//@RequestBody 使用json封装到参数,需要post请求
        //创建page对象
        Page<EduTeacher> page = new Page<>(current,limit);
        //MP 构建条件
        QueryWrapper<EduTeacher> wrapper =new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name))wrapper.like("name",name);
        if(!StringUtils.isEmpty(level))wrapper.eq("level",level);//等于
        if(!StringUtils.isEmpty(begin))wrapper.ge("gmt_create",begin);//大于
        if(!StringUtils.isEmpty(end))wrapper.le("gmt_create",end);//小于
        wrapper.orderByDesc("sort");
        teacherService.page(page,wrapper);

        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords(); //数据list集合

        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("添加教师接口方法")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        return save? R.ok():R.error();
    }

    @ApiOperation("根据id获取教师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return byId==null?R.error().message("该教师不存在"):R.ok().data(byId);
    }
    @ApiOperation("更新教师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        return b ? R.ok() : R.error();
    }

}

